
layui.use(['form','layer'], function () {

    var $ = layui.$, form = layui.form, layer=layui.layer;

    var positionList;
    var page = 1;
    var limit = 40;

    $(function () {
        LoginStatus();
        
        //加载职位信息
        loadPositionList();
    })
    
    //加载职位列表
    function loadPositionList() {
        
        layer.load(2);
        $.ajax({
            type: 'get', // 提交方式 get/post
            url: companyServices+"/position/list?page="+page+"&limit=" + limit, // 需要提交的 url
            contentType:"application/json;charset=utf-8",
            xhrFields: {
                withCredentials: true
            }
            ,crossDomain: true
            ,data:{},
            success: function(res) {
                layer.closeAll('loading');
                checkLoginStatus(res);

                if(JSON.parse(res).code == '0'){
                    positionList = JSON.parse(res).data;

                    var html = '';
                    if (positionList != null && positionList.length > 0) {
                        $.each(positionList, function (index, item) {
                            html += '<li><input type="checkbox" style="height:14px;width:14px;" name="job" value="'+item.id+'">';
                            html += '<div> <h2>'+item.positionName+'<span>'+dateFormat("yyyy-MM-dd",new Date(item.updateTime))+'</span></h2>';
                            html += '<p class="job-demand">';
                            html += '<span>'+((item.workCity == "" || item.workCity == null) ? "":item.workCity)+'</span>|<span>'+((item.workExperienceLevelName == "" || item.workExperienceLevelName == null) ? "":item.workExperienceLevelName)+'</span>|<span>'+((item.educationLevelName == "" || item.educationLevelName == null) ? "":item.educationLevelName)+'</span>';
                            html += '</p> <p class="pay">'+((item.salaryLevelName == "" || item.salaryLevelName==null) ? "":item.salaryLevelName)+'</p> </div> </li>';
                        })
                        
                        $('#positionList').html(html);
                    }
                }
            }, fail:function () {
                layer.closeAll('loading');
            }
        })
    }
    
    $('#batchDel').click(function () {
        layer.confirm('是否确认批量删除选中的职位？', {
            btn: ['确定','取消'] //按钮
        }, function(){
            delPosition()
        }, function(){

        });
    })
    
    function delPosition() {
        var ids = [];

        $.each($('input:checkbox:checked'),function(){
            ids.push($(this).val());
        });

        layer.load(2);
        $.ajax({
            type: 'delete', // 提交方式 get/post
            url: companyServices+"/position", // 需要提交的 url
            xhrFields: {
                withCredentials: true
            }
            ,crossDomain: true
            ,data: {ids:ids.join(',')}
            ,success: function(res) {
                layer.closeAll('loading');
                if(res.status == '00200'){
                    layerMsg(res.message);
                    setTimeout(function() {
                        window.location.href="wrap-job-manage.html" ;
                    }, 2000);

                }else{
                    layerMsg(res.message)
                }
            },fail:function () {
                layer.closeAll('loading');
            }
        })
    }

    

    /**************************************时间格式化处理************************************/
    function dateFormat(fmt,date) {
        var o = {
            "M+" : date.getMonth()+1,                 //月份
            "d+" : date.getDate(),                    //日
            "h+" : date.getHours(),                   //小时
            "m+" : date.getMinutes(),                 //分
            "s+" : date.getSeconds(),                 //秒
            "q+" : Math.floor((date.getMonth()+3)/3), //季度
            "S"  : date.getMilliseconds()             //毫秒
        };
        if(/(y+)/.test(fmt))
            fmt=fmt.replace(RegExp.$1, (date.getFullYear()+"").substr(4 - RegExp.$1.length));
        for(var k in o)
            if(new RegExp("("+ k +")").test(fmt))
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
        return fmt;
    }
});




